﻿using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.ComponentModel;

namespace OpenLightGroup.Common.Interactivity
{

    /// <summary>
    /// Allows a user to press the delete or backspace key to uncheck a selector
    /// </summary>
    [Description("Allows a user to press the delete or backspace key to uncheck a selector")]
    public class RemoveSelectorValueBehavior : Behavior<Selector>
    {

        /// <summary>
        /// Wires up the KeyUp event handler
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            this.AssociatedObject.KeyUp += new KeyEventHandler(AssociatedObject_KeyUp);

        }

        /// <summary>
        /// Removes the KeyUp event handler
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.AssociatedObject.KeyUp -= AssociatedObject_KeyUp;

        }

        private void AssociatedObject_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete || e.Key == Key.Back)
                this.AssociatedObject.SelectedIndex = -1;

        }

    }
}